package com.smart.wap.service;

import cn.hutool.core.collection.CollUtil;
import com.smart.manager.member.entities.enums.PigPlanStatus;
import com.smart.manager.member.entities.enums.PigPlanTime;
import com.smart.manager.member.entities.models.MemberMaterialPigPlan;
import com.smart.manager.member.entities.views.MemberPigView;
import com.smart.manager.member.entities.views.MemberView;
import com.smart.manager.product.entities.views.PropsComboView;
import io.ebean.DB;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 异步数据批量插入服务
 * @author Administrator
 */
@Service
public class BatchService {


    @Async
    public void addListPlan(List<MemberMaterialPigPlan> pigPlans, PropsComboView combo, Long memberId, Long pigId, Integer number) {

        LocalDate eatDate = LocalDate.now().plusDays(-1);
        if (CollUtil.isNotEmpty(pigPlans)) {
            eatDate = pigPlans.get(0).getEatDate();
        }
        List<MemberMaterialPigPlan> pigPlanList = new ArrayList<>();
        //计算当前时间
        LocalDate finish = eatDate.plusDays(number);
        while (eatDate.isBefore(finish)) {
            eatDate = eatDate.plusDays(1);
            Set<PigPlanTime> times = Set.of(PigPlanTime.L1, PigPlanTime.L2, PigPlanTime.L3);
            for (PigPlanTime time : times) {
                MemberMaterialPigPlan pigPlan = new MemberMaterialPigPlan().setStatus(PigPlanStatus.L2).setMember(new MemberView().setId(memberId)).setCombo(combo).setPigId(new MemberPigView().setId(pigId)).setEatDate(eatDate).setEatTime(time);
                pigPlanList.add(pigPlan);
            }
        }

        DB.insertAll(pigPlanList);

    }

}
